/**
 * Copyright (C) 2008 aileron.cc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package cc.aileron.web.sso;

import static cc.aileron.wsgi.WsgiHttpMethodType.GET;
import static cc.aileron.wsgi.WsgiHttpMethodType.POST;
import static cc.aileron.wsgi.container.utility.WsgiProcessRegistry.registry;
import cc.aileron.commons.resource.ResourceNotFoundException;
import cc.aileron.template.exception.CompileException;
import cc.aileron.web.sso.process.ConnectSessionProcess;
import cc.aileron.web.sso.process.RegistryCookieProcess;
import cc.aileron.web.sso.process.RegistrySessionProcess;
import cc.aileron.web.sso.process.ReissueTokenProcess;
import cc.aileron.web.sso.resources.OnetimeTokenResource;
import cc.aileron.web.sso.resources.SessionResource;
import cc.aileron.wsgi.WsgiProcess;
import cc.aileron.wsgi.WsgiSetting;
import cc.aileron.wsgi.container.WsgiContainer;
import cc.aileron.wsgi.provider.WsgiCookieProvider;
import cc.aileron.wsgi.provider.WsgiJsonProvider;
import cc.aileron.wsgi.provider.WsgiTemplateProvider;

import com.google.inject.Inject;

/**
 * ProcessSetting
 * @author Aileron
 *
 */
public class ProcessSetting implements WsgiSetting
{  
    /**
     * constractor injection
     * @param registrySessionProcess
     * @param registryCookieProcess
     * @throws ResourceNotFoundException
     * @throws CompileException
     */
    @Inject
    public ProcessSetting(
            final RegistrySessionProcess registrySessionProcess,
            final RegistryCookieProcess registryCookieProcess,
            final ReissueTokenProcess reissueTokenProcess,
            final ConnectSessionProcess connectSessionProcess,
            final WsgiCookieProvider cookieProvider,
            final WsgiTemplateProvider templateProvider,
            final WsgiJsonProvider jsonProvider)
        throws ResourceNotFoundException, CompileException
    {
        this.publicProcess = registryCookieProcess;
        this.privateProcess = registrySessionProcess;
        this.reissueTokenProcess = reissueTokenProcess;
        this.connectSessionProcess = connectSessionProcess;
        this.jsonTemplateProcess = jsonProvider.get();
        
        this.cookieProcess = cookieProvider.get("cookie");
        this.xmlTemplateProcess = templateProvider.get("private.xml", "text/xml;charset=UTF-8");
        this.publicTemplateProcess = templateProvider.get("public.text", "text/plain");
    }
    
/* 外部通信用 */
    
    /**
     * クッキーに書き込み
     */
    private final WsgiProcess<OnetimeTokenResource> cookieProcess;
    
    /**
     * 外部通信用
     */
    private final RegistryCookieProcess publicProcess;
    
    /**
     * 外部通信用テンプレート
     */
    private final WsgiProcess<OnetimeTokenResource> publicTemplateProcess;
    
    
/* セッション作成用、内部通信 */
    
    /**
     * テンプレートプロセス(json)
     */
    private final WsgiProcess<SessionResource> jsonTemplateProcess;
    
    /**
     * テンプレートプロセス(xml)
     */
    private final WsgiProcess<SessionResource> xmlTemplateProcess;
    
    /**
     * 内部通信用
     */
    private final RegistrySessionProcess privateProcess;
    
    /**
     * session id から session取得用
     */
    private final ConnectSessionProcess connectSessionProcess;
    
    /**
     * トークンの再発行
     */
    private final ReissueTokenProcess reissueTokenProcess;
    

    /*
     * (非 Javadoc)
     * @see cc.aileron.wsgi.WsgiSetting#setting(cc.aileron.wsgi.container.WsgiContainer)
     */
    @Override
    public void setting(final WsgiContainer container)
    {
        /**
         * 外部通信用
         */
        registry(OnetimeTokenResource.class, "").uriParameterKeys("tokenId")
        .uri("/public/cookie")
        .method(GET)
        .add(publicProcess)
        .add(cookieProcess)
        .add(publicTemplateProcess)
        .register(container);
        
        /**
         * 内部通信用
         */
        registry(SessionResource.class, "userIdentifier")
        .uri("/private/session")
        .method(POST)
        .add(privateProcess)
        .add(xmlTemplateProcess)
        .register(container);
        
        /**
         * 内部通信用 (session-idからsessionの取得)
         * XMLテンプレート
         */
        registry(SessionResource.class, "").uriParameterKeys("sessionId")
        .uri("/private/session").extension(".xml")
        .method(GET)
        .add(connectSessionProcess)
        .add(xmlTemplateProcess)
        .register(container);
        
        /**
         * 内部通信用　(session-idからsessionの取得)
         * jsonテンプレート
         */
        registry(SessionResource.class, "").uriParameterKeys("sessionId")
        .uri("/private/session").extension(".json")
        .method(GET)
        .add(connectSessionProcess)
        .add(jsonTemplateProcess)
        .register(container);
        
        /**
         * トークンの再発行
         */
        registry(SessionResource.class, "sessionId")
        .uri("/private/reissue-token")
        .method(POST)
        .add(reissueTokenProcess)
        .add(xmlTemplateProcess)
        .register(container);
    }
}